package com.houtai.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.houtai.commonutils.Result;
import com.houtai.eduservice.config.OperationLogDesc;
import com.houtai.eduservice.entity.UcenterMember;
import com.houtai.eduservice.entity.vo.UserQuery;
import com.houtai.eduservice.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-12-07
 */
@RestController
@RequestMapping( "/eduservice/ucenterMember")
//@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;
    //4.多条件组合查询带分页
    @ApiOperation(value = "多条件组合查询带分页")
    @OperationLogDesc(module = "用户模块",description = "查看用户列表")
    @PostMapping("pageUserCondition/{current}/{limit}")
    public Result pageUserCondition(@PathVariable long current,
                                       @PathVariable long limit,
                                       @RequestBody(required = false) UserQuery userQuery ){
        //创建page对象
        Page<UcenterMember> pageT = new Page<>(current,limit);
        //调用方法实现分页
        //构建条件
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        //wrapper
        //动态sql
        String mobile=userQuery.getMobile();
        Integer sex = userQuery.getSex();
        Integer age = userQuery.getAge();
        String begin = userQuery.getBegin();
        String end = userQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(mobile)){
            //构建条件
            wrapper.like("mobile",mobile);

        }
        if(!StringUtils.isEmpty(sex)){
            //构建条件
            wrapper.eq("sex",sex);

        }
        if(!StringUtils.isEmpty(begin)){
            //构建条件
            wrapper.gt("age",age);

        }
        if(!StringUtils.isEmpty(begin)){
            //构建条件
            wrapper.gt("gmt_create",begin);

        }
        if(!StringUtils.isEmpty(end)){
            //构建条件
            wrapper.le("gmt_create",end);

        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        ucenterMemberService.page(pageT,wrapper);

        long total = pageT.getTotal();//总记录数
        List<UcenterMember> records = pageT.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);
    }
    //2.逻辑删除(通过路径传入id)
    @ApiOperation(value = "逻辑删除")
    @OperationLogDesc(module = "用户模块",description = "删除用户信息")
    @DeleteMapping("{id}")
    public Result removeUser(@ApiParam(name="id",value = "用户ID",required = true) @PathVariable String id){
        boolean flag=ucenterMemberService.removeById(id);
        if(flag){
            return Result.ok();
        }
        else {
            return Result.error().message("删除失败！");
        }

    }

}

